package com.example.myapp.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapp.ADO.ActivityManager;
import com.example.myapp.ADO.MyDialog;
import com.example.myapp.R;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.util.V;

public class UPpwd extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  //隐藏标题栏
        setContentView(R.layout.uppwd);
        ImageView uppwd_back=findViewById(R.id.uppwd_back);
        EditText oldpwd=findViewById(R.id.oldpwd);
        EditText newpwd1 =findViewById(R.id.newpwd1);
        EditText newpwd2=findViewById(R.id.newpwd2);
        TextView uppwd_ok=findViewById(R.id.uppwd_ok);



        uppwd_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (newpwd1.getText().toString().trim().length() < 6) {
                    Toast.makeText(UPpwd.this, "密码不能少于6位数", Toast.LENGTH_LONG).show();
                } else {
                    if (!newpwd1.getText().toString().equals(newpwd2.getText().toString())) {
                        Toast.makeText(UPpwd.this, "两次密码不一致，请重试", Toast.LENGTH_LONG).show();
                    } else {

                        View view = getLayoutInflater().inflate(R.layout.exit_dialog_layout, null);
                        MyDialog mMyDialog = new MyDialog(UPpwd.this, 0, 0, view, R.style.DialogTheme);
                        TextView exitall=  view.findViewById(R.id.exitall);
                        TextView dialog_title= view.findViewById(R.id.dialog_title);
                        TextView exitback=  view.findViewById(R.id.exitback);
                        dialog_title.setText("确定修改吗？\n如果修改成功需要重新登录");
                        mMyDialog.setCancelable(true);
                        mMyDialog.show();
                        exitall.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                updatePassword(oldpwd.getText().toString(),newpwd1.getText().toString());
                                mMyDialog.dismiss();
                            }
                        });//退出程序
                        exitback.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mMyDialog.dismiss();
                            }
                        });//退出程序

                    }
                }


            }
        });
        uppwd_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    public void onBackPressed() {
        //此处写退向后台的处理
        finish();
    }//手机返回键

    /**
     * 提供旧密码修改密码
     */
    private void updatePassword(String oldpwd,String newpwd){
        //TODO 此处替换为你的旧密码和新密码
        BmobUser.updateCurrentUserPassword(oldpwd, newpwd, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(UPpwd.this, "修改密码成功", Toast.LENGTH_LONG).show();
                    BmobUser.logOut();
                    Intent intent = new Intent(UPpwd.this, Login.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(UPpwd.this, "旧密码密码不正确请重试", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
